/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：实现Resources动态加载管理器的主要逻辑
* CreateTime：2020-04-05 21:40:40
* Version：1.0
* Modify Recorder：
*************************************************/
using System;
using System.Collections.Generic;
using Object = UnityEngine.Object;

namespace TinyToolKit.AssetLoad
{
    public class TResourcesManager : ManagerBase 
    {
        private ResLoaderBase resLoader;

        public Dictionary<string, ResItem> AllAssets => resLoader.allAssets;

        #region Mono Func
        public override int Priority => 10;

        public override void Init()
        {
            resLoader = new ResourcesLoader();
        }

        public override void UpdateManager()
        {
            
        }

        public override void Shutdown()
        {
            resLoader.Clear();
        }
        #endregion

        #region Public Func
        /// <summary>
        /// 加载资源
        /// </summary>
        /// <param name="resPath">资源路径</param>
        /// <returns>返回加载的资源</returns>
        public Object LoadAsset(string resPath)
        {
            ResItem item = resLoader.LoadAsset(resPath);
            return item.objs?[0];
        }

        /// <summary>
        /// 加载资源
        /// </summary>
        /// <typeparam name="T">资源类型</typeparam>
        /// <param name="resPath">资源路径</param>
        /// <returns>返回加载的资源</returns>
        public T LoadAsset<T>(string resPath) where T : Object
        {
            ResItem item = resLoader.LoadAsset<T>(resPath);
            return item?.objs?[0] as T;
        }

        /// <summary>
        /// 加载指定文件夹路径下的所有的资源
        /// </summary>
        /// <param name="resPath">资源路径</param>
        /// <returns>返回加载的资源</returns>
        public Object[] LoadAllAssets(string resPath)
        {
            ResItem item = resLoader.LoadAllAssets(resPath);
            return item.objs;
        }

        /// <summary>
        /// 加载指定文件夹路径下的所有的资源
        /// </summary>
        /// <typeparam name="T">资源类型</typeparam>
        /// <param name="resPath">资源路径</param>
        /// <returns>返回加载的资源</returns>
        public T[] LoadAllAssets<T>(string resPath) where T : Object
        {
            ResItem item = resLoader.LoadAllAssets<T>(resPath);
            return item.objs as T[];
        }

        /// <summary>
        /// 异步加载资源
        /// </summary>
        /// <param name="resPath">资源路径</param>
        /// <param name="resType">资源类型</param>
        /// <param name="callBack">资源加载成功的回调函数</param>
        public void LoadAssetsAsync(string resPath, Type resType = null, Action<ResItem> callBack = null)
        {
            resLoader.LoadAssetsAsync(resPath, resType, callBack); 
        }

        /// <summary>
        /// 卸载资源，调用之前需要注意将所有加载出来的资源引用置空，否则无效
        /// </summary>
        public void UnloadUnusedAssets()
        {
            resLoader.UnloadUnusedAssets();
        }
        #endregion
    }
}

